package com.wgw.first.cart.utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.SparseArray;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wgw.first.home.bean.GoodssBean;
import com.wgw.first.utils.CacheUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Admin
 * @version $Rev$
 * @des ${TODO}
 * @updateAuthor $Author$
 * @updateDes ${TODO}
 */
public class CartStorage {
    public static final String JSON_CART = "json_cart";
    private Context context;
    //优化过的HashMap集合
    private SparseArray<GoodssBean.Goods> datas;

    private static CartStorage instance;

    private CartStorage(Context context) {
        this.context = context;
        datas = new SparseArray<>(100);
        listToSparse();
    }

    public static CartStorage getInstance(Context context) {
        if (instance == null) {
            instance = new CartStorage(context);
        }
        return instance;
    }

    //从本地读取的数据加入到sparseArray中
    private void listToSparse() {
        List<GoodssBean.Goods> carts = getAllData();
        //放到sparseArry中
        if (carts != null && carts.size() > 0) {
            for (int i = 0; i < carts.size(); i++) {
                GoodssBean.Goods goodsBean = carts.get(i);
                datas.put(Integer.parseInt(goodsBean.getISBN()),
                        goodsBean);
            }
        }
    }

    private List<GoodssBean.Goods> parsesToList() {
        List<GoodssBean.Goods> carts = new ArrayList<>();
        if (datas != null && datas.size() > 0) {
            for (int i = 0; i < datas.size(); i++) {
                GoodssBean.Goods shoppingCart = datas.valueAt(i);
                carts.add(shoppingCart);
            }
        }
        return carts;
    }

    public List<GoodssBean.Goods> getAllData() {
        return getDataFromLocal();
    }

    //本地获取json数据，并且通过Gson解析成list列表数据
    public List<GoodssBean.Goods> getDataFromLocal() {
        List<GoodssBean.Goods> carts = new ArrayList<>();
        //从本地获取缓存数据
        String savaJson = CacheUtils.getString(context, JSON_CART);
        if (!TextUtils.isEmpty(savaJson)) {
            //把数据转换成列表
            carts = new Gson().fromJson(savaJson, new TypeToken<List<GoodssBean.Goods>>() {
            }.getType());
        }
        return carts;
    }

    public void addData(GoodssBean.Goods cart) {
        //添加数据
        GoodssBean.Goods tempCart = datas.get(Integer.parseInt(cart.getISBN()));
        if (tempCart != null) {
            tempCart.setNumber(tempCart.getNumber() + 1);
        } else {
            tempCart = cart;
            tempCart.setNumber(1);
        }
        datas.put(Integer.parseInt(tempCart.getISBN()), tempCart);
        //保存数据
        commit();
    }

    //保存数据
    private void commit() {
        //把parseArray转换成list
        List<GoodssBean.Goods> carts = parsesToList();
        //把转换成String
        String json = new Gson().toJson(carts);
        // 保存
        CacheUtils.putString(context, JSON_CART, json);
    }

    public void deleteData(GoodssBean.Goods cart) {
        //删除数据
        datas.delete(Integer.parseInt(cart.getISBN()));
        //保存数据
        commit();
    }

    public void updataData(GoodssBean.Goods cart) {
        //修改数据
        datas.put(Integer.parseInt(cart.getISBN()), cart);
        //保存数据
        commit();
    }
}
